package com.uinnova.product.eam.mvc;

import com.binary.core.util.BinaryUtils;
import com.binary.framework.web.RemoteResult;
import com.uinnova.product.eam.model.es.SysApiKeyInfo;
import com.uinnova.product.eam.service.SysApiKeyService;
import com.uinnova.product.vmdb.comm.doc.annotation.ModDesc;
import com.uino.web.auth.VerifyAuthUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sys/apiKey")
public class SysApiKeyController {

    @Resource
    private SysApiKeyService sysApiKeyService;
    public static final String SYS_MODULE_SIGN = "系统APIKEY管理";
    @Resource
    private VerifyAuthUtil verifyAuth;

    @GetMapping("/list")
    @ModDesc(desc = "查询系统apiKey列表", pDesc = "", rDesc = "", rType = RemoteResult.class)
    public RemoteResult list() {
        verifyAuth.verifyAuth(SYS_MODULE_SIGN);
        List<SysApiKeyInfo> result = sysApiKeyService.list();
        return new RemoteResult(result);
    }

    @PostMapping("/saveOrUpdate")
    @ModDesc(desc = "保存、更新系统apiKey", pDesc = "sysApiKeyInfo", rDesc = "sysApiKeyInfo", rType = RemoteResult.class)
    public RemoteResult saveOrUpdate(@RequestBody SysApiKeyInfo sysApiKeyInfo) {
        verifyAuth.verifyAuth(SYS_MODULE_SIGN);
        BinaryUtils.checkEmpty(sysApiKeyInfo.getSysName(), "系统名称");
        SysApiKeyInfo result = sysApiKeyService.saveOrUpdate(sysApiKeyInfo);
        return new RemoteResult(result);
    }

    @PostMapping("/createApiKey")
    @ModDesc(desc = "生成/重新生成APIKEY", pDesc = "sysApiKeyInfo", rDesc = "sysApiKeyInfo", rType = RemoteResult.class)
    public RemoteResult createApiKey(@RequestBody SysApiKeyInfo sysApiKeyInfo) {
        verifyAuth.verifyAuth(SYS_MODULE_SIGN);
        BinaryUtils.checkEmpty(sysApiKeyInfo.getId(), "id");
        SysApiKeyInfo result = sysApiKeyService.createApiKey(sysApiKeyInfo);
        return new RemoteResult(result);
    }
}
